import tkinter.ttk as ttk

from select import select

from links.abstract_link_ui import AbstractUiLink


class SocketUI(AbstractUiLink):
    def __init__(self, parent):
        super().__init__(parent)
        self.server_address_label = None
        self.server_address_cb = None
        self.server_port_label = None
        self.server_port_sb = None
        self.write_to_label = None
        self.write_to_cb = None
        self.data_channel_label = None
        self.data_channel_cb = None
        self.check = None
        self.username_label = None
        self.username_entry = None
        self.password_label = None
        self.password_entry = None
        self.init_ui()

    def init_ui(self):
        row = 0
        server_address_label = ttk.Label(self, text="Server Address")
        server_address_label.grid(row=row, column=0, sticky="nsew", padx=2, pady=2)
        server_address_cb = ttk.Combobox(self, values=["127.0.0.1"])
        server_address_cb.grid(row=row, column=1, sticky="nsew", padx=2, pady=2)
        self.server_address_label = server_address_label
        self.server_address_cb = server_address_cb
        row += 1

        server_port_label = ttk.Label(self, text="Server Port")
        server_port_label.grid(row=row, column=0, sticky="nsew", padx=2, pady=2)
        server_port_sb = ttk.Spinbox(self, values=["5000"])
        server_port_sb.grid(row=row, column=1, sticky="nsew", padx=2, pady=2)
        self.server_port_label = server_port_label
        self.server_port_sb = server_port_sb
        row += 1

        write_to_label = ttk.Label(self, text="Write To")
        write_to_label.grid(row=row, column=0, sticky="nsew", padx=2, pady=2)
        write_to_cb = ttk.Combobox(self, values=["Server", "Client"])
        write_to_cb.grid(row=row, column=1, sticky="nsew", padx=2, pady=2)
        self.write_to_label = write_to_label
        self.write_to_cb = write_to_cb
        row += 1

        data_channel_label = ttk.Label(self, text="Data Channel")
        data_channel_label.grid(row=row, column=0, sticky="nsew", padx=2, pady=2)
        data_channel_cb = ttk.Combobox(self, values=["Bin", "Text"])
        data_channel_cb.grid(row=row, column=1, sticky="nsew", padx=2, pady=2)
        self.data_channel_label = data_channel_label
        self.data_channel_cb = data_channel_cb
        row += 1

        check = ttk.Checkbutton(self, text="Authentication")
        check.grid(row=row, column=0, columnspan=2, sticky="nsew", padx=2, pady=2)
        self.check = check
        row += 1

        username_label = ttk.Label(self, text="Username")
        username_label.grid(row=row, column=0, sticky="nsew", padx=2, pady=2)
        username_entry = ttk.Entry(self)
        username_entry.grid(row=row, column=1, sticky="nsew", padx=2, pady=2)
        self.username_label = username_label
        self.username_entry = username_entry
        row += 1

        password_label = ttk.Label(self, text="Password")
        password_label.grid(row=row, column=0, sticky="nsew", padx=2, pady=2)
        password_entry = ttk.Entry(self)
        password_entry.grid(row=row, column=1, sticky="nsew", padx=2, pady=2)
        self.password_label = password_label
        self.password_entry = password_entry
        row += 1

        self.grid_columnconfigure(1, weight=1)

    def hide_something_for_is_not_web_socket(self):
        self.data_channel_label.grid_remove()
        self.data_channel_cb.grid_remove()
        self.check.grid_remove()
        self.username_label.grid_remove()
        self.username_entry.grid_remove()
        self.password_label.grid_remove()
        self.password_entry.grid_remove()

    def hide_something_for_client(self):
        self.write_to_label.grid_remove()
        self.write_to_cb.grid_remove()

    def hide_something_for_server(self):
        self.hide_something_for_is_not_web_socket()
